package com.example.homework.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
public class Controller {
    private final Initial initial = new Initial();
    private final Customer customer = new Customer();
    private final Business business = new Business();
    private final Admin admin = new Admin();
    private int boss = 0; // 管理员登录
    private int bid = 0; // 登录店家
    private int cid = 0; // 登录顾客
    private int shop = 0; // 浏览店铺

    @PostMapping("/initial/register")
    @ResponseBody
    public Map<String, Object> register(int kind, String phone, String name,
                                                int sex, String pwd, String addr) {
        return initial.register(kind, phone, name, sex, pwd, addr);
    }

    @PutMapping("/initial/login")
    @ResponseBody
    public Map<String, Object> login(int kind, String name, String pwd) {
        Map<String, Object> ans = initial.login(kind, name, pwd);
        if ((int) ans.get("state") == 0) {
            if (kind == 0) {
                bid = initial.getLogin(kind, name, pwd);
            } else if (kind == 2) {
                boss = 1;
            } else {
                cid = initial.getLogin(kind, name, pwd);
            }
        }
        return ans;
    }

    @PutMapping("/initial/reset")
    @ResponseBody
    public Map<String, Object> reset(int kind, String tele, String prepwd,
                                             String pwd, String pwd2) {
        return initial.reset(kind, tele, prepwd, pwd, pwd2);
    }

    @PutMapping("/initial/logout")
    @ResponseBody
    public Map<String, Object> logout() {
        cid = bid = shop = boss = 0;
        Map<String, Object> map = new HashMap<>();
        map.put("state", 0);
        return map;
    }

    @PostMapping("/business/addfood")
    @ResponseBody
    public Map<String, Object> businessAddfood(String food, String price,
                                                byte[] img, String desc, int stock,
                                                String type, int rec, String status) {
        if (bid != 0) {
            return business.businessAddfood(bid, food, price, img, desc, stock, type, rec, status);
        }
        else { // 未登陆成功
            Map<String, Object> map = new HashMap<>();
            map.put("state", 1);
            return map;
        }
    }

    @PutMapping("/business/changefood")
    @ResponseBody
    public Map<String, Object> businessChangefood(int id, String food, String price,
                                                   String desc, int stock, String type,
                                                   int rec, String status) {
        if (bid != 0) {
            return business.businessChangefood(
                    bid, id, food, price, desc, stock, type, rec, status);
        }
        else { // 未登陆成功
            Map<String, Object> map = new HashMap<>();
            map.put("state", 1);
            return map;
        }
    }

    @DeleteMapping("/business/delfood")
    @ResponseBody
    public Map<String, Object> businessDelfood(int id) {
        if (bid != 0) {
            return business.businessDelfood(bid, id);
        }
        else { // 未登陆成功
            Map<String, Object> map = new HashMap<>();
            map.put("state", 1);
            return map;
        }
    }

    @PostMapping("/business/onefood")
    @ResponseBody
    public Map<String, Object> businessOnefood(int id) {
        if (bid != 0) {
            return business.businessOnefood(bid, id);
        }
        else { // 未登陆成功
            Map<String, Object> map = new HashMap<>();
            map.put("state", 1);
            return map;
        }
    }

    @PostMapping("/business/anyfood")
    @ResponseBody
    public ArrayList<Map<String, Object>> businessAnyfood(String id, String food, String type) {
        return business.businessAnyfood(bid, id, food, type);
    }

    @PostMapping("/business/allfood/detailed")
    @ResponseBody
    public ArrayList<Map<String, Object>> businessAllfood() {
        return business.businessAllfood(bid);
    }

    @PostMapping("/business/allorder")
    @ResponseBody
    public ArrayList<Map<String, Object>> businessAllorder() {
        return business.businessAllorder(bid);
    }

    @PostMapping("/business/anyorder")
    @ResponseBody
    public ArrayList<Map<String, Object>> businessAnyorder(String id, String cust) {
        return business.businessAnyorder(bid, id, cust);
    }

    @PostMapping("/business/acorder")
    @ResponseBody
    public Map<String, Object> businessAcorder(int id) {
        return business.businessAcorder(bid, id);
    }

    @PostMapping("/business/oneorder")
    @ResponseBody
    public Map<String, Object> businessOneorder(int id) {
        return business.businessOneorder(bid, id);
    }

    @PostMapping("/business/delorder")
    @ResponseBody
    public Map<String, Object> businessDelorder(int id) {
        return business.businessDelorder(bid, id);
    }

    @PostMapping("/business/timeorder")
    @ResponseBody
    public ArrayList<Map<String, Object>> businessTimeorder(String begin, String end) {
        return business.businessTimeorder(bid, begin, end);
    }

    @PutMapping("/customer/in")
    @ResponseBody
    public Map<String, Object> customerIn(String name) {
        Map<String, Object> ans = customer.customerIn(name);
        shop = (int) ans.get("shop");
        ans.remove("shop");
        return ans;
    }

    @PutMapping("/customer/out")
    @ResponseBody
    public Map<String, Object> customerOut() {
        shop = 0;
        Map<String, Object> map = new HashMap<>();
        map.put("state", 0);
        return map;
    }

    @GetMapping("/customer/allorder")
    @ResponseBody
    public ArrayList<Map<String, Object>> customerAllorder() {
        return customer.customerAllorder(cid);
    }

    @GetMapping("/customer/oneorder")
    @ResponseBody
    public Map<String, Object> customerOneorder(String order) {
        return customer.customerOneorder(cid, order);
    }

    @GetMapping("/customer/onefood")
    @ResponseBody
    public Map<String, Object> customerOnefood(String food, String id) {
        return customer.customerOnefood(shop, food, id);
    }

    @PostMapping("/customer/allfood/detailed")
    @ResponseBody
    public ArrayList<Map<String, Object>> customerAllfood() {
        return customer.customerAllfood(shop);
    }

    @GetMapping("/customer/allshop")
    @ResponseBody
    public ArrayList<Map<String, Object>> customerAllshop() {
        return customer.customerAllshop();
    }

    @GetMapping("/customer/oneshop")
    @ResponseBody
    public Map<String, Object> customerOneshop(String name) {
        return customer.customerOneshop(name);
    }

    @PostMapping("/customer/shop/cart/build")
    @ResponseBody
    public Map<String, Object> customerBuild() {
        return customer.customerBuild(shop, cid);
    }

    @PostMapping("/customer/shop/cart/submit")
    @ResponseBody
    public Map<String, Object> customerSubmit(String tips) {
        return customer.customerSubmit(shop, cid, tips);
    }

    @PutMapping("/customer/shop/cart/add")
    @ResponseBody
    public Map<String, Object> customerAdd(String name) {
        return customer.customerAdd(shop, cid, name);
    }

    @PutMapping("/customer/shop/cart/decrease")
    @ResponseBody
    public Map<String, Object> customerDecrease(String name) {
        return customer.customerDecrease(shop, cid, name);
    }

    @GetMapping("/customer/shop/cart/get")
    @ResponseBody
    public Map<String, Object> customerGet() {
        return customer.customerGet(shop, cid);
    }

    @PostMapping("/admin/allusers")
    @ResponseBody
    public ArrayList<Map<String, Object>> adminAllusers() {
        if (boss == 1) {
            return admin.adminAllusers();
        } else {
            return new ArrayList<>();
        }
    }

    @PostMapping("/admin/deluser")
    @ResponseBody
    public Map<String, Object> adminDeluser(int id) {
        if (boss == 1) {
            return admin.adminDeluser(id);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("state", 0);
            return map;
        }
    }

    @PostMapping("/admin/userpass")
    @ResponseBody
    public Map<String, Object> adminUserpass(int id) {
        if (boss == 1) {
            return admin.adminUserpass(id);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("state", 0);
            return map;
        }
    }

    @PostMapping("/admin/allfoods")
    @ResponseBody
    public ArrayList<Map<String, Object>> adminAllfoods() {
        if (boss == 1) {
            return admin.adminAllfoods();
        } else {
            return new ArrayList<>();
        }
    }

    @PostMapping("/admin/foodpass")
    @ResponseBody
    public Map<String, Object> adminFoodpass(int id) {
        if (boss == 1) {
            return admin.adminFoodpass(id);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("state", 0);
            return map;
        }
    }

    @PostMapping("/admin/delfood")
    @ResponseBody
    public Map<String, Object> adminDelfood(int food_id, int seller_id) {
        if (boss == 1) {
            return admin.adminDelfood(food_id, seller_id);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("state", 0);
            return map;
        }
    }

    @PostMapping("/admin/allorders")
    @ResponseBody
    public ArrayList<Map<String, Object>> adminAllorders() {
        if (boss == 1) {
            return admin.adminAllorders();
        } else {
            return new ArrayList<>();
        }
    }

    @PostMapping("/admin/orderpass")
    @ResponseBody
    public Map<String, Object> adminOrderpass(int id) {
        if (boss == 1) {
            return admin.adminOrderpass(id);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("state", 0);
            return map;
        }
    }

    @PostMapping("/admin/delorder")
    @ResponseBody
    public Map<String, Object> adminDelorder(int id) {
        if (boss == 1) {
            return admin.adminDelorder(id);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("state", 0);
            return map;
        }
    }
}